package com.guowen.myaccount;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.guowen.myaccount.adapter.AccManagementAdapter;
import com.guowen.myaccount.db.AccManageBean;
import com.guowen.myaccount.db.DBManager;
import com.guowen.myaccount.utils.AccountSettings;

import java.util.List;

public class AccManagementActivity extends AppCompatActivity implements View.OnClickListener{

    private List<AccManageBean> mDatas;
    public static AccManagementAdapter adapter;
    public static ListView accManageLv;
    private ImageView backIv, addIv;
    public static TextView sumTv;
    private TextView tranTv;
    public static Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_acc_management);
        initView();
        loadData();
    }


    private void loadData() {

        sumTv.setText(DBManager.getSumMoneyAll()+"");
        mDatas = DBManager.getAccManList();
        adapter = new AccManagementAdapter(this,mDatas);
        accManageLv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    public void initView() {

        context = this;
        backIv = findViewById(R.id.activity_accmanage_back);
        addIv = findViewById(R.id.activity_accmanage_iv);
        sumTv = findViewById(R.id.activity_accmanage_sum);
        accManageLv = findViewById(R.id.activity_accmanage_lv);
        tranTv = findViewById(R.id.activity_accmanage_tv_tran);

        addIv.setOnClickListener(this);
        backIv.setOnClickListener(this);
        tranTv.setOnClickListener(this);

        // 给ListView添加监听器
        accManageLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AccManageBean accManageBean = mDatas.get(position);
                Intent intent = new Intent(AccManagementActivity.this, AccountSettings.class);
                intent.putExtra("position",position);
                intent.putExtra("type",1);//告诉活动面，此时位修改的形式 1为修改 0为添加

                AccManagementActivity.this.startActivity(intent);
            }
        });

        accManageLv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

                mDatas = DBManager.getAccManList();
                AccManageBean bean = mDatas.get(position);

                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("提示").setMessage("您确定要删除这个名为\""+bean.getTypename()+"\"的账户行嘛？")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mDatas.remove(bean);
                                DBManager.deleteItemFromAccmanagetbById(bean.getId());
                                loadData();
                            }
                        }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create().show();

                return true;
            }
        });
    }


    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()) {
            case R.id.activity_accmanage_back:
                finish();
                break;
            case R.id.activity_accmanage_iv:
                // 添加一种账户
                intent = new Intent(this,AccountSettings.class);
                startActivity(intent);
                break;
            case R.id.activity_accmanage_tv_tran:
                intent = new Intent(this,TranAccountActivity.class);
                startActivity(intent);
                break;

        }
    }
}